/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taskmanager;

import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Application;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;

/**
 *
 * @author denaas
 */
public class TaskMonitor implements Runnable{
    private List<TaskInfo> taskChain; 
    private AlarmManager alarmManager;
    private Controller controller;
    private int period;
    
    
    /**Constructor
     * 
     * @param list task list
     * @param manager alarm manager
     * @param controller  controller
     */
    public TaskMonitor(List list, AlarmManager manager, Controller controller){
        taskChain = list;
        alarmManager = manager;
        this.controller = controller;
        this.period = controller.getPeriod();
    }
    
    /**
     * Checks the due date of the task and alarms if expired
     * 
     *
     */
    private void Check(){
        Date now = new Date();
        for(TaskInfo task: taskChain){
            Date theDate = task.getDueDate();
            if (!task.isClosed() && theDate.before(now))
            {
                alarmManager.alarm(task);
                controller.closeTask(task.getId());
                controller.setMustUpdate(true);
            }
        }
    }
       
    public void run(){
        while (true){
            Check();
            try {
                Thread.sleep(period);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
